package com.boboPlanet.comm.rabbitmq;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description: TODO 消费失败处理策略  Direct
 * @Author: mis_wu
 * @Date: 2023/6/27
 * @Company planet
 **/
@Configuration
public class MqConsumeErrorConfig {

    @Bean
    public Queue errorQueue(){
        return new Queue(PlanetCommMQEnum.PLANET_ERROR_MQ.getQueue(),true);
    }

    @Bean
    DirectExchange errorExchange() {
        return new DirectExchange(PlanetCommMQEnum.PLANET_ERROR_MQ.getExchange(),true,false);
    }

    /**
     * todo:路由绑定交换机(routing-key)
     * @return Binding
     */
    @Bean
    Binding errorBing() {
        return BindingBuilder.bind(this.errorQueue())
                .to(this.errorExchange())
                .with(PlanetCommMQEnum.PLANET_ERROR_MQ.getRoutingKey());
    }

    @Bean
    public MessageRecoverer recover(RabbitTemplate rabbitTemplate){
        return new RepublishMessageRecoverer(rabbitTemplate,PlanetCommMQEnum.PLANET_ERROR_MQ.getExchange(),PlanetCommMQEnum.PLANET_ERROR_MQ.getRoutingKey());
    }

}
